#!/usr/bin/python3
import RPi.GPIO as RG
import time

BuzzerPin = 17


def setup(pin):
    global BuzzerPin
    BuzzerPin = pin
    RG.setmode(RG.BCM)
    RG.setup(BuzzerPin, RG.OUT)
    RG.output(BuzzerPin, RG.HIGH)


def on():
    print("低电压")
    RG.output(BuzzerPin, RG.LOW)


def off():
    print("高电压")
    RG.output(BuzzerPin, RG.HIGH)


def beep(x):
    on()
    time.sleep(x)
    off()
    time.sleep(x)


def loop():
    while True:
        beep(2)


def destroy():
    RG.output(BuzzerPin, RG.HIGH)
    RG.cleanup()  # Release resource


if __name__ == '__main__':  # Program start from here
    setup(17)
    try:
        loop()
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destroy()
